package com.hdax.gulimall.product.service.impl;

import com.hdax.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
import org.mockito.internal.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.Query;

import com.hdax.gulimall.product.dao.PmsBrandDao;
import com.hdax.gulimall.product.entity.PmsBrandEntity;
import com.hdax.gulimall.product.service.PmsBrandService;


@Service("pmsBrandService")
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandDao, PmsBrandEntity> implements PmsBrandService {


    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key= (String) params.get("key");
        QueryWrapper<PmsBrandEntity> brandEntityQueryWrapper=new QueryWrapper<PmsBrandEntity>();
        if(!StringUtils.isEmpty(key)){
            brandEntityQueryWrapper.eq("brand_id",key).or().like("name",key);
        }

        IPage<PmsBrandEntity> page = this.page(
                new Query<PmsBrandEntity>().getPage(params),
                brandEntityQueryWrapper
        );

        return new PageUtils(page);
    }


    /**
     * 修改品牌以及品牌关联的表
     * @param pmsBrand
     */
    @Override
    public void updateByIdDetail(PmsBrandEntity pmsBrand) {

        //保证冗余字段的数据一致
        //通过品牌的Id来修改品牌
        boolean b = this.updateById(pmsBrand);
        //如果修改了name属性，那么品牌关联其他表的name也同样要发生改变
        if(!StringUtils.isEmpty(pmsBrand.getName())){
            //修改品牌关联分类表的name
            categoryBrandRelationService.updateBrand(pmsBrand.getBrandId(),pmsBrand.getName());
        }


        //TODO更新其他关联

    }


}